var wxParam = {};
var wx_share = function(param, url) {
    $.ajax({
        url:  "/weChat/wxShare",
        type: "GET",
        data: url,
        success: function(data) {
        	var result = data.data;
            wxParam = param;
            // 分享配置
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: result.appId, // 必填，企业号的唯一标识
                timestamp: result.timestamp, // 必填，生成签名的时间戳
                nonceStr: result.nonceStr, // 必填，生成签名的随机串
                signature: result.signature,// 必填，签名，见附录1
                jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'] // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
            });
            wx.ready(function(){showWxFQ();});
        },  
        error : function(res) {}
    });
}

wx_share({
    title: "小学生数学报在线课堂",
    desc: "最具权威的小学生在线学习课程，等你加入",
    link: "http://whxf.pinjingpay.com/",
    imgUrl: "http://whxf.pinjingpay.com/static/images/h02.jpg",
    type:"link",
    successFn: function(){
    	//alert("分享成功");
    },
    cancelFn: function(){
        // 用户取消分享后执行的回调函数
    	//alert("取消分享");
    }
},{"url":location.href.split('#').toString()});

function showWxFQ(){
    //分享到朋友圈
    wx.onMenuShareTimeline({
        title: wxParam.title,
        link: wxParam.link,
        imgUrl: wxParam.imgUrl,
        success: function () {
           // if (wxParam.successFn) {wxParam.successFn();}
        	var resJson = eval("("+JSON.stringify(res)+")");
        }, cancel: function () {
            //if (wxParam.cancelFn) {wxParam.cancelFn();}
        	var resJson = eval("("+JSON.stringify(res)+")");
        }
    });
    
    //分享给朋友
    wx.onMenuShareAppMessage({
        title: wxParam.title,
        desc: wxParam.desc,
        link: wxParam.link,
        imgUrl: wxParam.imgUrl,
        type: wxParam.type,
       // dataUrl: wxParam.dataUrl,
        success: function () {
        	var resJson = eval("("+JSON.stringify(res)+")");
        	//console.log(resJson);
        	
        }, cancel: function () {
        	var resJson = eval("("+JSON.stringify(res)+")");
        	//console.log(resJson);
        }
    });
    
    //分享到QQ
    wx.onMenuShareQQ({
        title: wxParam.title,
        desc: wxParam.desc,
        link: wxParam.link,
        imgUrl: wxParam.imgUrl,
        success: function () {
        	var resJson = eval("("+JSON.stringify(res)+")");
        }, cancel: function () {
        	var resJson = eval("("+JSON.stringify(res)+")");
        }
    });
    
    //分享到腾讯微博
    wx.onMenuShareWeibo({
        title: wxParam.title,
        desc: wxParam.desc,
        link: wxParam.link,
        imgUrl: wxParam.imgUrl,
        success: function () {
        	var resJson = eval("("+JSON.stringify(res)+")");
        }, cancel: function () {
        	var resJson = eval("("+JSON.stringify(res)+")");
        }
    });
    
    //分享到QQ空间
    wx.onMenuShareQZone({
        title: wxParam.title,
        desc: wxParam.desc,
        link: wxParam.link,
        imgUrl: wxParam.imgUrl,
        success: function () {
        	var resJson = eval("("+JSON.stringify(res)+")");
        }, cancel: function () {
        	var resJson = eval("("+JSON.stringify(res)+")");
        }
    });
}

/**
 * 微信分享路径
 * @param appid 微信公众号id
 * @param url 分享路径
 * @returns {String}
 */
function wxShareUrl(url){
	return "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx379887ab70b5abd7&redirect_uri="+encodeURIComponent(url)+"&response_type=code&scope=snsapi_userinfo&state=hello-world&connect_redirect=1#wechat_redirect";
}
